package com.ttbj.product.zwrefund.wxpay;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.SortedMap;

 
public class PayCommonUtil {
	
	/**
	 * 验证签名
	 * @param characterEncoding
	 * @param packageParams
	 * @param API_KEY
	 * @return
	 */
	public static boolean isTenpaySign(String characterEncoding, SortedMap<Object, Object> packageParams, String API_KEY) {  
        StringBuffer sb = new StringBuffer();  
        Set es = packageParams.entrySet();  
        Iterator it = es.iterator();  
        while(it.hasNext()) {  
            Map.Entry entry = (Map.Entry)it.next();  
            String k = (String)entry.getKey();  
            String v = (String)entry.getValue();  
            if(!"sign".equals(k) && null != v && !"".equals(v)) {  
                sb.append(k + "=" + v + "&");  
            }  
        }  
        sb.append("key=" + API_KEY);  
        //算出摘要  
        String mysign = MD5Util.md5(sb.toString(), characterEncoding);
        String tenpaySign = ((String)packageParams.get("sign"));  
        return tenpaySign.equals(mysign);  
    }  
	
	
	/**
	 * sign签名 使用md5加密
	 * @param characterEncoding
	 * @param packageParams
	 * @param api_key
	 * @return
	 */
	public static String createSign(String characterEncoding, SortedMap<Object, Object> packageParams, String api_key){
		StringBuffer sBuffer = new StringBuffer();
		Iterator<Map.Entry<Object, Object>> it = packageParams.entrySet().iterator();
		while(it.hasNext()){
			Map.Entry<Object, Object> entry = it.next();
			String key = (String) entry.getKey();
			String val = (String) entry.getValue();
			if (null !=val && !"".equals(val) && !"sign".equals(key) && !"key".equals(key)) {
				sBuffer.append(key + "=" + val + "&");
			}
		}
		sBuffer.append("key=" + api_key);
		String sign = MD5Util.md5(sBuffer.toString(), characterEncoding);
		return sign;
	}
	
	/**
	 * 将请求参数转换为xml格式的string
	 * @param params
	 * @return
	 */
	public static String getRequestXml(SortedMap<Object, Object> params){
		StringBuffer sBuffer = new StringBuffer();
		sBuffer.append("<xml>");
		Iterator<Map.Entry<Object, Object>> it = params.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<Object,Object> entry =  it.next();
			String key = (String) entry.getKey();
			String val = (String) entry.getValue();
			if ("attch".equalsIgnoreCase(key) || "body".equalsIgnoreCase(key) || "sign".equalsIgnoreCase(key)) {
				sBuffer.append("<"+key+">" + "<![CDATA["+val+"]]></"+key+">");
			}else{
				sBuffer.append("<"+key+">" + val + "</"+key+">");
			}
		}
		sBuffer.append("</xml>");
		return sBuffer.toString();
	}
	
	/**
	 * 取出一个指定长度的正整数
	 * @param length
	 * @return
	 */
	public static int bulidRandom(int length) {
		int num = 1;
		double random = Math.random();
		if (random < 0.1) {
			random = random + 1;
		}
		for(int i=0; i<length; i++) {
			num = num * 10;
		}
		return (int)(random * num);
	}
	
	/**
	 * 获取当前时间
	 * @return
	 */
	public static String getCurrTime() {
		return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	}
	
	/**
	 * 随机数
	 * @return
	 */
	public static String getNonce_str() {
		String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		Random random = new Random();
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < 15; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}
	
	


}
